package net.huashitong.question.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.security.SysPrincipal;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.ObjectUtils;

import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.entity.TertiaryIndustry;
import net.huashitong.Indicators.service.TertiaryIndustryService;
import net.huashitong.question.entity.Coordinate;
import net.huashitong.question.filter.CoordinateFilter;
import net.huashitong.question.service.CoordinateService;
import net.huashitong.question.service.SupervisionTaskService;
import net.huashitong.taskjob.entity.TaskJob;
import net.huashitong.taskjob.service.ResDpteService;
import net.huashitong.taskjob.service.SupervisionService;
import net.huashitong.taskjob.service.TaskJobService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/questionReport")
public class QuestionReport extends JsonBaseController {
    @Autowired
    SupervisionTaskService supervisionTaskService;
    @Autowired
    CoordinateService coordinateService;
    @Autowired
    TaskJobService taskJobService;
    @Autowired
    private SupervisionService supervisionService;
    @Autowired
    private ResDpteService resDpteService;
    @Autowired
    private TertiaryIndustryService tertiaryIndustryService;

    @Override
    protected String getView(String s) {
        return "/modules/sys/question/" + s;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String initPage() {

        return getView("query");
    }

    //申请协调
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Message create(@ModelAttribute CoordinateFilter filter) {
//        filter.setUserId(SysUserUtils.getUserId());
//        Coordinate coordinate = new Coordinate();
//        BeanUtils.copyProperties(filter, coordinate);
//        coordinate.setAaa001("1");//1 有效。
//        coordinateService.save(coordinate);
        coordinateService.saveProblemTask(filter);
        return coverMessage("200");
    }

    /**
     * 申请协调
     *
     * @param model
     * @return url
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showAddView(@ModelAttribute CoordinateFilter filter, Model model) {
        //查询所有督查组
        List<SysDept> supervisionList = supervisionService.queryAllSupervisions();
        //查询所有责任单位
        List<SysDept> resDepts = resDpteService.queryAllResDepts();
        //查询所有主要经济社会发展指标
        List<TertiaryIndustry> industries = tertiaryIndustryService.queryByIndicatorLevel("");
        //上报人
        SysPrincipal bnpUser = SysUserUtils.getSysPrincipal();

        model.addAttribute("industries", industries);
        model.addAttribute("supervisionList", supervisionList);
        model.addAttribute("resDepts", resDepts);
        model.addAttribute("coordinate", filter);
        model.addAttribute("bnpUser", bnpUser);
        return getView("createTask");
    }

    //审核转批
    @RequestMapping(value = "/edit1/{id}", method = RequestMethod.GET)
    public String showEditView1(Model model, @PathVariable String id) {
        //查询所有督查组
        List<SysDept> supervisionList = supervisionService.queryAllSupervisions();
        //查询所有责任单位
        List<SysDept> resDepts = resDpteService.queryAllResDepts();
        Coordinate coordinate = coordinateService.getById(id);

        model.addAttribute("supervisionList", supervisionList);
        model.addAttribute("resDepts", resDepts);
        model.addAttribute("coordinate", coordinate);
        return getView("form1");
    }


    /**
     * 协调事项处理
     *
     * @param coordinate
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit1", method = RequestMethod.POST)
    public Message update1(@ModelAttribute CoordinateFilter coordinate) {

        TaskJob task = new TaskJob();
        BeanUtils.copyProperties(coordinate, task);

        //生成督查
        task.setAaa001("1");//有效任务
        task.setTaskType("2");//问题转批生成
        task.setTaskResult("0");
        task.setRespResult("0");
        task.setRespScore("0.0");
        task.setSupervisionResult("0");
        task.setSupervisionScore("0.0");
        task.setIsFeedback("0");
        task.setTaskTimes("1");

        task.setTaskName(coordinate.getProblemName());
        task.setTaskContent(coordinate.getBaseInfo()+coordinate.getHelpInfo());
        task.setStartDate(coordinate.getPlanStartDate());
        task.setIsRebuild("0");

        //保存责任单位coordinate
        if(StringUtils.isNotBlank(coordinate.getDeptId())){
            SysDept sysDept = resDpteService.getById(coordinate.getDeptId());
            task.setResDept(sysDept.getDeptName());
        }
        //保存督查组
        if(StringUtils.isNotBlank(coordinate.getGroupCode())){
            SysDept sysDept = resDpteService.getById(coordinate.getGroupCode());
            task.setGroupName(sysDept.getDeptName());
        }

        //发短信通知督查任务生成
        task.setAaa002("1");
        taskJobService.save(task);

        Coordinate coor = coordinateService.getById(coordinate.getSysId());
        if (ObjectUtils.isNotBlank(coor)) {
            coor.setAaa001("2");//生成督查任务
            coor.setTaskId(task.getSysId());
            coordinateService.update(coor);
        } else {
            return coverMessage("500", "找不到该协调事项");
        }
        return coverMessage("200");
    }

    @RequestMapping("/filter")
    public String filter(@ModelAttribute CoordinateFilter filter,Model model) {
        //查询所有督查组
        List<SysDept> supervisionList = supervisionService.queryAllSupervisions();
        //查询所有责任单位
        List<SysDept> resDepts = resDpteService.queryAllResDepts();
        //查询所有主要经济社会发展指标
        List<TertiaryIndustry> industries = tertiaryIndustryService.queryByIndicatorLevel("");
        //上报人
        SysPrincipal bnpUser = SysUserUtils.getSysPrincipal();

        model.addAttribute("industries", industries);
        model.addAttribute("supervisionList", supervisionList);
        model.addAttribute("resDepts", resDepts);
        model.addAttribute("coordinate", filter);
        model.addAttribute("bnpUser", bnpUser);
        return getView("filter");
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute CoordinateFilter filter) {
        List<Coordinate> lists = coordinateService.queryCoordinate(filter);
        return coverJqGrid(filter, lists);
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String showEditView(Model model, @PathVariable String id) {
        //查询所有督查组
        List<SysDept> supervisionList = supervisionService.queryAllSupervisions();
        //查询所有责任单位
        List<SysDept> resDepts = resDpteService.queryAllResDepts();
        Coordinate coordinate = coordinateService.getById(id);

        model.addAttribute("supervisionList", supervisionList);
        model.addAttribute("resDepts", resDepts);
        model.addAttribute("coordinate", coordinate);
        return getView("form");
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Message update(@ModelAttribute CoordinateFilter filter) {

        Coordinate coordinate = coordinateService.getById(filter.getSysId());
        Coordinate newCoordinate = new Coordinate();
        //不能修改指标
        BeanUtils.copyProperties(filter, newCoordinate);
        newCoordinate.setIndustryId(coordinate.getIndustryId());
        newCoordinate.setAaa001(coordinate.getAaa001());
        //保存责任单位
        if(StringUtils.isNotBlank(filter.getDeptId())){
            SysDept sysDept = resDpteService.getById(filter.getDeptId());
            newCoordinate.setDeptName(sysDept.getDeptName());
        }
        //保存督查组
        if(StringUtils.isNotBlank(filter.getGroupCode())){
            SysDept sysDept = resDpteService.getById(filter.getGroupCode());
            newCoordinate.setGroupName(sysDept.getDeptName());
        }
        coordinateService.update(newCoordinate);
        return coverMessage("200");
    }

    @ResponseBody
    @RequestMapping(value = "/cancelProblem/{id}")
    public Message cancelProblem(@PathVariable List<String> id){
        List<Coordinate> coordinates = new ArrayList<>();
        for(String str : id){
            Coordinate coordinate = coordinateService.getById(str);
            coordinate.setAaa001("3");
            TaskJob taskJob = taskJobService.getById(coordinate.getTaskId());
            taskJob.setAaa001("0");//任务失效
            taskJobService.update(taskJob);
            coordinates.add(coordinate);
        }
        coordinateService.batchUpdate(coordinates);
        return coverMessage("200");
    }

}
